import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;
import java.util.List;
import java.util.Locale;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

//import org.apache.catalina.core.ApplicationContext;

import com.kylin.common.util.Config;
import com.kylin.common.util.MemObjFactory;
import com.kylin.common.util.UTool;


public class InitServlet extends HttpServlet {
	protected static Logger log = Logger.getLogger(InitServlet.class);
	protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1)
			throws ServletException, IOException {
		doPost(arg0, arg1);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		Locale sessionLocale = (Locale) request.getSession().getAttribute(
				org.apache.struts.Globals.LOCALE_KEY);
		if(sessionLocale == null){
			sessionLocale = Locale.getDefault();
			 request.getSession().setAttribute(org.apache.struts.Globals.LOCALE_KEY,sessionLocale);
		}
		String cmd= request.getParameter("CMD");
		if("reloadAll".equals(cmd)){
			loadConfig(getServletContext());
			MemObjFactory.AllCountryMap = new Hashtable();
			for(int i=0;i<MemObjFactory.SysLocales.length; i++){
				MemObjFactory.getAllCountryList(MemObjFactory.SysLocales[i]);
				out.println("<BR>getAllCountry("+MemObjFactory.SysLocales[i].getDisplayLanguage(sessionLocale)+")");
			}
			MemObjFactory.AllLanguageMap = new Hashtable();
			for(int i=0;i<MemObjFactory.SysLocales.length; i++){
				MemObjFactory.getAllLanguageList(MemObjFactory.SysLocales[i]);
				out.println("<BR>getAllLanguage("+MemObjFactory.SysLocales[i].getDisplayLanguage(sessionLocale)+")");
			}
		}
		String clearName = request.getParameter("clearName");
		response.getWriter().println("Clear " + clearName + " and reload ok");
	}

	public void init() throws ServletException {
		System.out.println("\n=============== init =============\n=");
		System.out.println("=    加载 配置文件");
		loadConfig(getServletContext());

		System.out.println("\n=============== init =============\n=");
		System.out.println("=    加载 系统数据字典");

		for (int i = 0; i < MemObjFactory.SysLocales.length; i++) {
			MemObjFactory.getAllCountryList(MemObjFactory.SysLocales[i]);
			MemObjFactory.getAllLanguageList(MemObjFactory.SysLocales[i]);
			MemObjFactory.getSysLanguageList(MemObjFactory.SysLocales[i]);
		}
		MemObjFactory.loadDicts();
		System.out.println("=    加载  完成");

		/*
		PropertyFileReader pfr = PropertyFileReader.getInstance();
		boolean b = pfr.LoadFile();
		if (b) {
			String str = pfr.getPropertyValues("name");
			System.out.println("name is " + str);
		}
		*/
		// ApplicationContext one = new ApplicationContext

		System.out.println("=\n============== init over==============");

	}

	private void loadConfig(ServletContext context) {
		String configPath = context.getRealPath("/WEB-INF/") + File.separator + "kylin-config.xml";
		try {
			Config.load(configPath);
			log.info("加载配置文件完成：" + configPath);
			Config.homePath = context.getRealPath("/");
			Config.uploadFileDir = Config.homePath + Config.getString("uploadFileDir");
			Config.uploadMaxSize = Config.getInt("uploadMaxSize",100);
			List languageList = Config.getList("sys-languages.language");
			Config.allowImageTypes = Config.getList("fileType.imgType.ext");
			Config.allowMediaTypes = Config.getList("fileType.mediaType.ext");

			for(int i=0;i<Config.allowImageTypes.size();i++){
				log.info("允许上传的图片类型：" + Config.allowImageTypes.get(i));
			}
			for(int i=0;i<Config.allowMediaTypes.size();i++){
				log.info("允许上传的媒体文件类型：" + Config.allowMediaTypes.get(i));
			} 
			MemObjFactory.SysLocales = new Locale[languageList.size()];
			for(int i=0;i<languageList.size();i++){
				MemObjFactory.SysLocales[i] = UTool.getLocaleByLang(languageList.get(i).toString());
				log.info("系统支持语言：" + MemObjFactory.SysLocales[i].getDisplayName());
			}
			log.info("上传文件存放根路径     : " + Config. uploadFileDir);
			log.info("上传文件最大尺寸: " + Config.uploadMaxSize );
		} catch (Exception e) {
			log.error("加载配置文件出错：" + e);
			e.printStackTrace();
		}
	}
}
